package net.ufrog.common.xls.disposer;

import net.ufrog.common.exception.ServiceException;
import net.ufrog.common.utils.Calendars;
import net.ufrog.common.utils.Strings;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;

import java.util.Date;

/**
 * 日期解析器
 * 
 * @author ultrafrog
 * @version 1.0, 2015-09-01
 * @since 1.0
 */
public class DateDisposer extends Disposer {

    @Override
    public Object get(Cell cell, net.ufrog.common.xls.annotation.Cell annotation) {
        return cell.getDateCellValue();
    }

    @Override
    public void set(Cell cell, Object value, net.ufrog.common.xls.annotation.Cell annotation) {
        cell.setCellType(CellType.STRING);
        if (value instanceof Date) {
            cell.setCellValue(Calendars.format(Strings.empty(annotation.format(), "yyyy/M/d HH:mm"), (Date) value));
        } else if (value instanceof String) {
            cell.setCellValue(String.valueOf(value));
        } else {
            throw new ServiceException("value '" + value.getClass().getName() + "' is not date.");
        }
    }
}
